home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / vms / getline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  1.9 KB  |  74 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. /* CHANGED FOR VMS */
  20.  
  21. /*
  22.  * <getline.c>
  23.  */
  24.  
  25. #include <stddef.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <errno.h>
  29.  
  30. /* Read up to (and including) a newline from STREAM into *LINEPTR
  31.    (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
  32.    NULL), pointing to *N characters of space.  It is realloc'd as
  33.    necessary.  Returns the number of characters read (not including the
  34.    null terminator), or -1 on error or EOF.  */
  35.  
  36. int getline(char **lineptr, size_t *n, FILE *stream)
  37. {
  38. static char line[256];
  39. char *ptr;
  40. unsigned int len;
  41.  
  42.    if (lineptr == NULL || n == NULL)
  43.    {
  44.       errno = EINVAL;
  45.       return -1;
  46.    }
  47.  
  48.    if (ferror (stream))
  49.       return -1;
  50.  
  51.    if (feof(stream))
  52.       return -1;
  53.      
  54.    fgets(line,256,stream);
  55.  
  56.    ptr = strchr(line,'\n');   
  57.    if (ptr)
  58.       *ptr = '\0';
  59.  
  60.    len = strlen(line);
  61.    
  62.    if ((len+1) < 256)
  63.    {
  64.       ptr = realloc(*lineptr, 256);
  65.       if (ptr == NULL)
  66.          return(-1);
  67.       *lineptr = ptr;
  68.       *n = 256;
  69.    }
  70.  
  71.    strcpy(*lineptr,line); 
  72.    return(len);
  73. }
  74.